<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingServer;
use app\common\model\ConsultingServerSku;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;
use app\index\traits\ConsultingTrait;
use app\index\traits\MutedTrait;

/**
 * Class FreeConsultingService
 * @package app\consulting\service
 */
class FreeConsultingService
{
    use MutedTrait;
    use ConsultingTrait;

    // dao
    private ConsultingDao $consultingDao;

    public function __construct(ConsultingDao $consultingDao)
    {
        $this->consultingDao = $consultingDao;
    }

    /**
     * 创建咨询 V1.0
     * @param UM $user
     * @param array $params
     * @return  Consulting
     * @deprecated
     */
    // public function add(UM $user, array $params): Consulting
    // {
    //     $memberId = $user->id;
    //
    //     $lawyerId = $params['lawyerId'] ?? 0;
    //     $lawyerId = is_id($lawyerId) ? $lawyerId : 0;
    //     $matchType = is_id($lawyerId) ? Consulting::MATCH_TYPE_DESIGNATE : Consulting::MATCH_TYPE_RANDOM;
    //     $pagePath = $params['pagePath'] ?? '';
    //     $categoryId = $params['categoryId'] ?? '0';
    //     $consultingFormId = $params['consultingFormId'] ?? '0';
    //
    //     // 渠道链路
    //     ['pf' => $pf, 'chain' => $chain, 'promotionGroupId' => $promotionGroupId] = getChainParams();
    //
    //     // 咨询创建者
    //     $this->checkConsultingCreator($user);
    //
    //     // 禁言
    //     $this->checkMuted($user);
    //
    //     // 咨询创建次数
    //     $this->checkConsultingTimes($user);
    //
    //     // 免费咨询服务
    //     $freeServer = ConsultingServer::where('serverType', ConsultingServer::FREE)->find();
    //
    //     // 创建咨询
    //     $record = $this->addConsulting($freeServer, null, Consulting::BT_CONSULTING, $memberId, $matchType, $lawyerId,
    //         '0', '0', '', $consultingFormId, $categoryId, $user->provinceId, $user->cityId, $pf, $chain, $promotionGroupId, $pagePath);
    //
    //     if ($record->isNew == 1) {
    //
    //         // 指定律师加入
    //         if (is_id($lawyerId)) {
    //             $this->getConsultingLawyerMatchService()->matchDesignate($record, $lawyerId);
    //         }
    //
    //         // 咨询创建事件
    //         event('ConsultingCreated', $record);
    //
    //         // 用户加入咨询事件
    //         event('ConsultingMemberJoined', $record);
    //     }
    //
    //     return $record;
    // }

    /**
     * 插入咨询
     * @param ConsultingServer|null $server
     * @param ConsultingServerSku|null $serverSku
     * @param string $businessType
     * @param $memberId
     * @param $matchType
     * @param $lawyerId
     * @param $lawsuitId
     * @param $landingId
     * @param $landingType
     * @param $consultingFormId
     * @param $categoryId
     * @param $provinceId
     * @param $cityId
     * @param $pagePath
     * @return Consulting|null
     */
    public function addConsulting(?ConsultingServer $server, ?ConsultingServerSku $serverSku, string $businessType, $memberId,
    $initMatchType, $matchType, $lawyerId, $lawsuitId, $landingId, $landingType, $consultingFormId, $categoryId, $provinceId, $cityId, $pf, $chain, $promotionGroupId, $pagePath): ?Consulting
    {
        $consultingServerId = $server ? $server->id : '0';
        $consultingServerSkuId = $serverSku ? $serverSku->id : '0';
        $serverType = $server->serverType;
        $consultingType = 0;
        $status = 1;

        //  初始付费服务
        if ($server && $serverType != ConsultingServer::FREE) {
            $consultingType = 1;
            $status = 0;
        }

        // 插入数据
        $consultingParams = [
            'categoryId' => $categoryId,
            'memberId' => $memberId,
            'lawyerId' => $lawyerId,
            'robotId' => 1,
            'initMatchType' => $initMatchType,
            'matchType' => $matchType,
            'businessType' => $businessType,
            'consultingType' => $consultingType,
            'consultingServerId' => $consultingServerId,
            'consultingServerSkuId' => $consultingServerSkuId,
            'pagePath' => $pagePath,
            'lawsuitId' => $lawsuitId,
            'landingId' => $landingId,
            'landingType' => $landingType,
            'consultingFormId' => $consultingFormId,
            'lawyerMoney' => 0,
            'status' => $status,
            'isAsked' => 0,
            'isAnswered' => 0,
            'ask' => '',
            'questionsNum' => 0,
            'isChatFreely' => 0,
            'isChatFreelyEndTime' => 0,
            'freeNum' => 0,
            'provinceId' => $provinceId,
            'cityId' => $cityId,
            'rushTimes' => 0,
            'lastActiveTime' => time() + 24 * 3600,
            'isEntry' => 0,
            'pf' => $pf,
            'chain' => $chain,
            'promotionGroupId' => $promotionGroupId,
        ];

        return $this->consultingDao->findORInsertConsulting($consultingParams);
    }

}